/**
 	InterfaceRO class
 */
package nrb.utils
{
	import flash.display.Sprite;
	import flash.events.*;
	import flash.net.*;
	import nrb.utils.MessageBox;
	
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	/**
	 	Type: Class
	 	@Params:none
	 	@return:none
	 	Desc: Class for communicating flex to server side that returns AMF(Action Message Format) using RemoteObject
	 */
    public class InterfaceRO extends Sprite {
		
		/**
		 	variables for remote object, listener
		 */
		private var ro:RemoteObject;
		private var listener:Function;
		
		/**
		 	Type: Controller
		 	@Params:none
		 	@return:none
		 	Desc: Initializes variables and configure listener
		 */
        public function InterfaceRO() {
            ro = new RemoteObject();
            listener = new Function();
        }
        
        /**
		 	Type: Public Function
		 	@Params:obj[{destination:String, source:String, method:String, params:Object, listener:Function}] AS Object
		 	@return:void
		 	Desc: calls specific controller and method in server side
		 */
        public function process(obj:Object): void {
        	this.listener = obj.listener;
        	ro.destination = obj.destination;
        	ro.source = obj.source;
        	configureListeners(ro.getOperation(obj.method));
 
        	ro.addEventListener(FaultEvent.FAULT, faultHandler);
        	ro.addEventListener(ProgressEvent.PROGRESS, progressHandler);
        	if(obj.params != null)
        		ro.getOperation(obj.method).send(obj.params);
        	else
        		ro.getOperation(obj.method).send();
        }
        
        /**
		 	Type: Private Function
		 	@Params:dispatcher[IEventDispatcher] AS IEventDispatcher
		 	@return:void
		 	Desc: adds listeners to method call
		 */
        private function configureListeners(dispatcher:IEventDispatcher):void {
             dispatcher.addEventListener(ResultEvent.RESULT, completeHandler);
             //dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
        }
		
		/**
		 	Type: Private Function
		 	@Params:event[ResultEvent] AS ResultEvent
		 	@return:void
		 	Desc: listener for successful transaction with the server side
		 */
        private function completeHandler(event:ResultEvent):void {
            if(event.result != null){
            	var obj:Object = new Object;
            	obj = event.result;
            	if(this.listener != null)
            		this.listener(obj);
            }
            //for debugging
            //Alert.show(event.message.toString()); 	
        }
		
		/**
		 	Type: Private Function
		 	@Params:event[ProgressEvent] AS ProgressEvent
		 	@return:void
		 	Desc: listener for progress
		 */
        private function progressHandler(event:ProgressEvent):void {
            Alert.show("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
        }
		
		/**
		 	Type: Private Function
		 	@Params:event[SecurityErrorEvent] AS SecurityErrorEvent
		 	@return:void
		 	Desc: listener for security error (eg: sandbox violation - cannot access file due to permissions)
		 */
        private function securityErrorHandler(event:SecurityErrorEvent):void {
            Alert.show("securityErrorHandler: " + event);
        }
        
        /**
		 	Type: Private Function
		 	@Params:event[faultHandler] AS faultHandler
		 	@return:void
		 	Desc: listener for errors with the remote object 
		 */
        private function faultHandler(event:FaultEvent): void {
        	Alert.show(event.fault.faultString);
        }

        
		
		
	}
}